﻿var m_project_id = '';
var m_xkzh = '';
var m_obj = null;
var objOpinion = null;
var vUserName = null;
var vData = null;
var dlgShowHeight = ($(window).height() - 220) / 2 + window.parent.pageYOffset;
var dlgShowWidth = ($(window).width() - 550) / 2 + window.parent.pageXOffset;
$("document").ready(function () {
    m_project_id = $.query.get("Project_ID");
    var blhj = $.query.get("BLHJ");
    $(":button").each(function () {
        if ($(this).attr("id").indexOf("_") > 0) {
            $(this).css("display", "none");
        }
    });
    $("select").each(function () {
        if ($(this).attr("id").indexOf("_") > 0) {
            $(this).css("display", "none");
        }
    });
    $("span").css("display", "none");
    var objSel = null;
    var objSave = null;
    var objDel = null;
    $("input").attr("disabled", "disabled");
    $(":text").css("background-color", "#E4E4E4");
    $("textarea").attr("disabled", "disabled");
    $("textarea").css("background-color", "#E4E4E4");
    $("select").attr("disabled", "disabled");
    $("select").css("background-color", "#E4E4E4");
    $("input").attr("readonly", "readonly");
    $("textarea").attr("readonly", "readonly");
    $("#btnSave").removeAttr("disabled");

    $("table").css("display", "none");
    $("#MainTable3").css("display", "inline");
    $("#txtOpinionModelName").removeAttr("readonly");
    $("#txtOpinionModelName").removeAttr("disabled");
    $("#btnSaveOpinionModel").removeAttr("disabled");

    if (blhj != '') {
        if (blhj == "PHFZ") {
            $("#btnRKGD").removeClass("nosee");
            $("#btnRKGD").removeAttr("disabled");
            $("table").css("display", "inline");
            $("#btnSave").css("display", "none");
        } else {
            $("[tag='" + blhj + "']").removeAttr("disabled");
            $("[tag='" + blhj + "']").removeAttr("readonly");
            $("[tag='" + blhj + "']").css("background-color", "white");
            $("input.dateinput[tag='" + blhj + "']").click(function () { WdatePicker({ dateFmt: 'yyyy-M-d' }); });
            //给审批人赋默认值（如果为空，给登录人名称）
            $('span[name="' + blhj + '"]').css("display", "inline");
            objSel = $("#selOpinionModel_" + blhj + "");
            objSave = $("#btnOpenOpinionModel_" + blhj + "");
            objDel = $("#btnDelOpinionModel_" + blhj + "");
            objOpinion = $("textarea[tag='" + blhj + "']");
            $(objSel).removeAttr("disabled");
            $(objSel).css("background-color", "white");
            $(objSave).removeAttr("disabled");
            $(objDel).removeAttr("disabled");
            $(objSel).css("display", "inline");
            $(objSave).css("display", "inline");
            $(objDel).css("display", "inline");
            vUserName = $("[tname='" + blhj + "']");
            vData = $(objOpinion);
            switch (blhj) {
                case "SCSP":
                    $("#tblSCSP").css("display", "block");
                    var vsfhz = $.query.get("SFHZ");
                    if (vsfhz == "1") {
                        $("#tblDHCSCYJ").css("display", "block");
                        $("#tblKGCYJ").css("display", "block");
                        $("#tblZSBSCYJ").css("display", "block");
                        $("#tblGHCSCYJ").css("display", "block");
                        $("#tblDJCSCYJ").css("display", "block");
                    }
                    break;
                case "DHCSCYJ":
                    $("#tblSCSP").css("display", "block");
                    $("#tblDHCSCYJ").css("display", "block");
                    break;
                case "KGCYJ":
                    $("#tblSCSP").css("display", "block");
                    $("#tblKGCYJ").css("display", "block");
                    break;
                case "ZSBSCYJ":
                    $("#tblSCSP").css("display", "block");
                    $("#tblZSBSCYJ").css("display", "block");
                    break;
                case "GHCSCYJ":
                    $("#tblSCSP").css("display", "block");
                    $("#tblGHCSCYJ").css("display", "block");
                    break;
                case "DJCSCYJ":
                    $("#tblSCSP").css("display", "block");
                    $("#tblDJCSCYJ").css("display", "block");
                    break;
                case "FH":
                    $("#tblSCSP").css("display", "block");
                    $("#tblDHCSCYJ").css("display", "block");
                    $("#tblKGCYJ").css("display", "block");
                    $("#tblZSBSCYJ").css("display", "block");
                    $("#tblGHCSCYJ").css("display", "block");
                    $("#tblDJCSCYJ").css("display", "block");
                    $("#tblFH").css("display", "block");
                    break;
                case "SH":
                    $("#tblSCSP").css("display", "block");
                    $("#tblDHCSCYJ").css("display", "block");
                    $("#tblKGCYJ").css("display", "block");
                    $("#tblZSBSCYJ").css("display", "block");
                    $("#tblGHCSCYJ").css("display", "block");
                    $("#tblDJCSCYJ").css("display", "block");
                    $("#tblFH").css("display", "block");
                    $("#tblSH").css("display", "block");
                    break;
                case "FGJZYJ":
                    $("#tblSCSP").css("display", "block");
                    $("#tblDHCSCYJ").css("display", "block");
                    $("#tblKGCYJ").css("display", "block");
                    $("#tblZSBSCYJ").css("display", "block");
                    $("#tblGHCSCYJ").css("display", "block");
                    $("#tblDJCSCYJ").css("display", "block");
                    $("#tblFH").css("display", "block");
                    $("#tblSH").css("display", "block");
                    $("#tblFGJZYJ").css("display", "block");
                    break;
                case "JZBGHYJ":
                    $("#tblSCSP").css("display", "block");
                    $("#tblDHCSCYJ").css("display", "block");
                    $("#tblKGCYJ").css("display", "block");
                    $("#tblZSBSCYJ").css("display", "block");
                    $("#tblGHCSCYJ").css("display", "block");
                    $("#tblDJCSCYJ").css("display", "block");
                    $("#tblFH").css("display", "block");
                    $("#tblSH").css("display", "block");
                    $("#tblFGJZYJ").css("display", "block");
                    $("#tblJZBGHYJ").css("display", "block");
                    break;
            }
        }
    } else {
        $("input.dateinput").click(function () { WdatePicker({ dateFmt: 'yyyy-M-d' }); });
    }
    $("#btnPrint").removeAttr("disabled");
    if (m_project_id != null && m_project_id != '') {
        var dbalias = "CKQDJK";
        var tablename = "采矿申请登记";
        var condittion = "PROJECT_ID=\\\'" + m_project_id + "\\\'";
        m_obj = new SmObjectData("CKQDJK", "采矿申请登记");
        m_obj.GetFirstSmObject(condittion);
        InitSQB();
    }
    //加载审批意见模板
    var strSCYJMB = "<option value=''></option><option value='资料符合要求'>审查意见1</option><option value='资料不符合要求'>审查意见2</option><option value='资料不齐，请补齐后提交'>审查意见3</option>"
    var strFHYJMB = "<option value=''></option><option value='已核'>复核意见1</option><option value='资料不符合要求'>复核意见2</option><option value='资料不齐'>复核意见3</option>"
    var strSHYJMB = "<option value=''></option><option value='同意'>审核意见1</option><option value='不同意'>审核意见2</option>"
    var strQFYJMB = "<option value=''></option><option value='同意'>签发意见1</option><option value='不同意'>签发意见2</option>"
    $("#selSCYJ").html(strSCYJMB);
    $("#selFHYJ").html(strFHYJMB);
    $("#selSHYJ").html(strSHYJMB);
    $("#selQF").html(strQFYJMB);
    //选择审批意见，更新审批意见内容
    $("#selSCYJ").change(function () {
        $("#txt审查人意见").val($(this).val());
    });
    $("#selFHYJ").change(function () {
        $("#txtFHYJ").val($(this).val());
    });
    $("#selSHYJ").change(function () {
        $("#txtSHYJ").val($(this).val());
    });
    $("#selQF").change(function () {
        $("#txtQFYJ").val($(this).val());
    });

    var dlgZC1 = null;
    if (objSave != null) {
        $(objSave).click(function () {
            var dlgShowTOP = 100 + $(window.parent).scrollTop();
            var dlgShowWidth = ($(window).width() - 550) / 2 + window.parent.pageXOffset;

            dlgZC1 = $("#dlg-SelectStaff1").dialog({ modal: true, title: "填写要保存的意见模版名称", width: "550px", position: [dlgShowWidth, dlgShowTOP] });
            dlgZC1.dialog("open");
            $("#txtOpinionModelName").val($("#" + $(objSel).attr("id") + " option:selected").text());
        });
    }

    if (objSel != null) {
        //意见模版。
        BindOpinionModelList(objSel);
        $(objSel).change(function () {
            if ($(this).val() == "") return;
            var vId = $(objSel).val();
            var url = DBCenterServiceUrl + "/WLMQ_OpinionModel/WLMQ_OpinionModel.asmx/GetOpinionModelContent";
            var param = "{'ID':" + vId + "}";
            GetSmJSON(url, param, function (str) {
                $(objOpinion).val(str);
            });
        });
    }
    if (objDel != null) {
        $(objDel).click(function () {
            if (!confirm("确定要删除此条审批意见？"))
                return;
            var obj = $(objSel);
            var vId = $(obj).val();
            if (vId == "" || vId == "0") return;
            var url = DBCenterServiceUrl + "/WLMQ_OpinionModel/WLMQ_OpinionModel.asmx/DeleteOpinionModel";
            var param = "{'ID':" + vId + "}";
            GetSmJSON(url, param, function (json) {
                if (json != "") {
                    alertex(json);
                    $(objOpinion).val("");
                    BindOpinionModelList(obj);
                }
            });
        });
    }

    $("#btnSaveOpinionModel").click(function () {
        var objId = $(objSel).attr("id");
        var vName = $("#txtOpinionModelName").val();
        if (vName.trim() == "") {
            alertex("意见模版名称不能为空，请填写后保存。", true);
            return;
        }
        var vId = $(objSel).val() == null ? "0" : $(objSel).val();
        var vModelContent = $(objOpinion).val();
        var m_OpinionModel = new SmObjectData("CKQDJK", "意见模板");
        m_OpinionModel.GetFirstSmObject("ID=0");
        m_OpinionModel.SetValue("ID", vId);
        m_OpinionModel.SetValue("员工ID", m_user.StaffID);
        m_OpinionModel.SetValue("工作流ID", "0");
        m_OpinionModel.SetValue("模板名称", vName);
        m_OpinionModel.SetValue("模板内容", vModelContent);
        m_OpinionModel.SetValue("部门ID", "0");
        m_OpinionModel.SetValue("备注", "");

        alertex(m_OpinionModel.Update(), true);
        BindOpinionModelList($(objSel));
        if (dlgZC1 != null) {
            dlgZC1.dialog("close");
            dlgZC1 = null;
        }
        //选中刚刚修改或者增加的意见
        if (vId == 0) {
            var vCnt = $("#" + objId + ">option").length - 1;
            $("#" + objId + ">option").eq(vCnt).prop("selected", true);
        } else {
            $("#" + objId + " option").each(function () {
                if ($(this).val() == vId) {
                    $(this).attr("selected", "true");
                    return;
                }
            });
        }
    });

    $(".m_textarea,.m_textarea_big").dblclick(function () {
        $(this).toggleClass("m_textarea_big2", 1000);
    });
});
function InitSQB() {
    var obj = m_obj;
    var m_staff = new SmWFStaff();
    var m_date = new SmDate();
    m_date.GetServerDate();
    m_staff.GetCurrentUser();
    var m_rq = m_date.Value;
    var m_name = m_staff.StaffName;
    for (i = 0; i < obj.Data.Data.length; i++) {
        var key = obj.Data.Data[i].Key;
        var value = obj.Data.Data[i].Value;
        if ($("[name='txt" + key + "']") != null) {
            $("[name='txt" + key + "']").val(value);
        }
    }
    //给审批时间赋默认值（如果为空，调服务器时间）
    var mrsj = ["txtSCSJ", "txtDHCSCRQ", "txtKGCSCRQ", "txtZSBSCRQ", "txtGHCSCRQ", "txtDJCSCRQ", "txtFHSJ", "txtSHSJ", "txt签发时间", "txtJZBGHSCRQ"];
    for (var i = 0; i < mrsj.length; i++) {
        if ($("input[name='" + mrsj[i] + "']").val() == "" && $("input[name='" + mrsj[i] + "']").attr("disabled") != "disabled") {
            //$("input[name='" + mrsj[i] + "']").val(m_rq);
            //给审批时间赋默认值（如果为空，调服务器时间）
            GetNowDate($("input[name='" + mrsj[i] + "']"));
        }
    }
    //给审批人赋默认值（如果为空，给登录人名称）
    var mrspr = ["txt审查人", "txtFH", "txtSH", "txtQF", "txtDHCSCR", "txtKGCSCR", "txtZSBSCR", "txtGHCSCR", "txt签发", "txtJZBGHSCR", "txtDJCSCR"];
    for (var j = 0; j < mrspr.length; j++) {
        if ($("input[name='" + mrspr[j] + "']").val() == "" && $("input[name='" + mrspr[j] + "']").attr("disabled") != "disabled") {
            $("input[name='" + mrspr[j] + "']").val(m_name);
        }
    }
}

function SetFMReadonly() {
    $("input").attr("readonly", "readonly");
}

function btnSave_Click() {
    if (objOpinion == null || vUserName == null || vData == null) return;
    if ($(objOpinion).val().trim() == "") {
        alertex("意见不能为空", true);
        return false;
    }
    if ($(vUserName).val().trim() == "") {
        alertex("审查人不能为空", true);
        return false;
    }
    if ($(vData).val().trim() == "") {
        alertex("审查时间不能为空", true);
        return false;
    }
    var str = "审查人意见,SCR,SCSJ,DHCSCYJ,DHCSCR,DHCSCRQ,KGCYJ,KGCSCR,KGCSCRQ,ZSBSCYJ,ZSBSCR,ZSBSCRQ,GHCSCYJ,GHCSCR,GHCSCRQ,DJCSCYJ,DJCSCR,DJCSCRQ,FHYJ,复核,FHSJ,SHYJ,SH,SHSJ,FGJZYJ,签发,签发时间,QFYJ,JZBGHYJ,JZBGHSCR,JZBGHSCRQ"
    if (m_obj != null) {
        var strs = str.split(',');
        for (i = 0; i < strs.length; i++) {
            m_obj.SetValue(strs[i], $("[name='txt" + strs[i] + "']").val() == null ? "" : $("[name='txt" + strs[i] + "']").val());
        }
        alertex(m_obj.Update(), true);
    }
}

function BLWC() {
    var url = DBCenterServiceUrl + "/KZ_SPDJ/KZ_SPDJ_CKQBG.asmx/FinishProject";
    var data = "{'project_id':'" + m_project_id + "'}";
    connectajax(url, null, data, true, null);
    alertex("入库归档成功", true);
}
function ReplyPrintClick() {
    window.open("../SP_COMMON/PrintPage/CKQXL_SPZRB.shtml?PROJECT_ID=" + m_project_id + "&TITLE=采矿权变更审批责任表");
}

function BindOpinionModelList(obj) {
    var url = DBCenterServiceUrl + "/WLMQ_OpinionModel/WLMQ_OpinionModel.asmx/GetOpinionModelList";
    var param = "{'UserId':" + m_user.StaffID + ",'WorkflowID':0,'DepartmentID':0}";
    GetSmJSON(url, param, function (json) {
        callback(json, obj);
    });
}

function callback(json, obj) {
    var data = JSON.parse(json);
    if (data.Rows.length == 0) return;
    $("#" + obj.attr("id") + " option").each(function () {
        $(this).remove();
    });
    var vhtml = '<option value="0"></option>';
    for (var i = 0; i < data.Rows.length; i++) {
        vhtml += '<option value="' + data.Rows[i][0] + '">' + data.Rows[i][2] + '</option>';
    }
    $(obj).html(vhtml);
}